<template>
  <div class="article-comments">
    <!-- 评论列表 -->
   <!-- {{articleId}} -->
    <van-list
      v-model="loading"
      :finished="finished"
      finished-text="没有更多了"
      @load="onLoad"
    >
      <van-cell
        v-for="item in list"
        :key="item.com_id.toString()"
      >
        <van-image
          slot="icon"
          round
          width="30"
          height="30"
          style="margin-right: 10px;"
          :src="item.aut_photo"
        />
        <span style="color: #466b9d;" slot="title">{{item.aut_name}}</span>
        <div slot="label">
          <p style="color: #363636;">{{item.content}}</p>
          <p>
            <span style="margin-right: 10px;">{{item.pubdate | relativeTime}}</span>
            <van-button size="mini" type="default" @click="showComments(item)">回复{{item.reply_count}}</van-button>
          </p>
        </div>
        <van-icon slot="right-icon" name="like-o" />
      </van-cell>
    </van-list>
    <!-- 评论列表 -->

    <!-- 发布评论 -->
    <van-cell-group class="publish-wrap">
      <van-field
        clearable
        placeholder="请输入评论内容"
        v-model.trim="content"
      >
        <van-button slot="button" size="mini" type="info" @click="addComment">发布</van-button>
      </van-field>
    </van-cell-group>
    <!-- /发布评论 -->
    <!-- 评论回复的弹出层 -->
    <van-popup v-model="show" round  position="bottom" :style="{ height: '80%' }" >
      <!-- <comment-reply @close="show = false" v-if="show" :currentComment="currentComment"></comment-reply> -->
      <comment-reply :show.sync="show" v-if="show" :currentComment="currentComment"></comment-reply>
    </van-popup>
  </div>
</template>

<script>
import { reqGetComments, reqAddComment } from '@/api/article'
import CommentReply from './comment-reply'
export default {
  name: 'ArticleComment',
  props: {
    // 文章id
    id: {
      type: String,
      required: true
    }
  },
  components: { CommentReply },
  data () {
    return {
      num: 1,
      loading: false,
      finished: false,
      list: [],
      offset: null,
      content: '', // 输入框值
      show: false, // 评论回复的弹出层
      currentComment: {} // 点击回复按钮要回显的评论项
    }
  },
  methods: {
    async onLoad () {
      // last_id 是下次取数据的时候赋值给offset
      const { data: { results, last_id } } = await reqGetComments('a', this.id, this.offset, 10)
      //  存储数据
      this.list.push(...results)
      // 赋值offset
      this.offset = last_id
      // 手动关闭loading
      this.loading = false
      // 结束状态
      if (results.length === 0) {
        this.finished = true
      }
    },
    // 添加评论  1324173911989944320
    async addComment () {
      try {
        if (this.content === '') return
        const { data: { new_obj } } = await reqAddComment(this.id, this.content)
        // console.log(new_obj)
        // 将新增的评论直接前置追加给 list数组
        this.list.unshift(new_obj)
        // 清空输入框
        this.content = ''
        // 提示用户
        this.$toast.success('发表成功')
      } catch (error) {
        this.$toast('该文章禁止评论')
      }
    },
    // 展示评论回复的弹出层
    showComments (obj) {
      this.show = true
      this.currentComment = obj
    }
  }
}
</script>

<style scoped lang='scss'>
// 发表评论的区域是固定在下端的
.publish-wrap {
  position: fixed;
  left: 0;
  bottom: 0;
  width: 100%;
}
// 给发表评论区空出地方
.van-list {
  margin-bottom: 45px;
}
</style>
